﻿@using Newbe.BookmarkManager.Services
@using Newbe.BookmarkManager.Services.EventHubs
@using WebExtensions.Net
<Panel Header="🔍 Omnibox Suggest" Key="🔍 Omnibox Suggest" @ref="_panel">
    <ExtraTemplate>
        <Switch @bind-Value="@UserOptions.OmniboxSuggestFeature.Enabled"
                CheckedChildren="Enabled"
                UnCheckedChildren="Disabled"
                OnChange="OnEnableChange"/>
    </ExtraTemplate>
    <ChildContent>
        @if (FeatureEnabled)
        {
            <Text>Max Suggest Count</Text>
            <AntDesign.InputNumber DefaultValue="Consts.Omnibox.SuggestDefault"
                                   Min="Consts.Omnibox.SuggestMin"
                                   Max="Consts.Omnibox.SuggestMax"
                                   @bind-Value="@UserOptions.OmniboxSuggestFeature.SuggestCount">

            </AntDesign.InputNumber>
        }
        else
        {
            <Paragraph>Enter the "af" keyword in the Browser's address bar, and AF can provide suggestions in response</Paragraph>
            <Paragraph>
                you can find more details at <a href="https://af.newbe.pro/docs/02-2-Searching#address-bar-search">Search in omnibox</a>
            </Paragraph>
        }
    </ChildContent>
</Panel>

@code
{
    [Parameter]
    public ControlPanel.ModalModel UserOptions { get; set; } = new();

    private Panel _panel = null!;

    private bool FeatureEnabled => UserOptions?.OmniboxSuggestFeature?.Enabled == true;

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        _panel.Disabled = !FeatureEnabled;
        if (!FeatureEnabled)
        {
            _panel.Active = true;
        }
    }

    private async Task OnEnableChange(bool oldEnabled)
    {
        if (!oldEnabled)
        {
            _panel.Disabled = false;
            _panel.Active = true;
        }
        else
        {
            _panel.Disabled = true;
            _panel.Active = false;
        }
        StateHasChanged();
    }
}